/**
 * API使用示例
 * 展示如何使用新的签名算法API
 * 
 * 签名算法说明：
 * 第一次签名: md5(url + "&" + timespan + "&" + queryParams + "&" + bodyContent + "&key=" + secretKey)
 * 最终签名: md5(TenantId + firstSign + timestamp + authorization)
 */

import API from '@/utils/api.js'
import CONFIG from '@/utils/apiConfig.js'

// 示例1: GET请求
// 签名过程: /api/user/info&1640995200&userId=123&key=your_secret_key
export const getUserInfo = (userId) => {
	return API.get(CONFIG.url.userInfo, { userId });
};

// 示例2: POST请求（JSON格式）
// 签名过程: /api/user/login&1640995200&&{"username":"test","password":"123456"}&key=your_secret_key
export const login = (username, password) => {
	return API.post(CONFIG.url.login, {
		username,
		password
	});
};

// 示例3: POST请求（表单格式）
// 签名过程: /api/user/login&1640995200&&username=test&password=123456&key=your_secret_key
export const loginForm = (username, password) => {
	return API.formPost(CONFIG.url.login, {
		username,
		password
	});
};

// 示例4: PUT请求
export const updateUserInfo = (userInfo) => {
	return API.put(CONFIG.url.userInfo, userInfo);
};

// 示例5: DELETE请求
export const deleteOrder = (orderId) => {
	return API.delete(CONFIG.url.orders, { orderId });
};

// 示例6: 带查询参数的GET请求
// 签名过程: /api/orders/list&1640995200&page=1&size=10&status=pending&key=your_secret_key
export const getOrders = (page = 1, size = 10, status = '') => {
	return API.get(CONFIG.url.orders, {
		page,
		size,
		status
	});
};

// 示例7: 复杂POST请求
// 签名过程: /api/orders&1640995200&&{"goods":[...],"address":{...},"remark":"备注"}&key=your_secret_key
export const createOrder = (orderData) => {
	return API.post(CONFIG.url.orders, {
		goods: orderData.goods,
		address: orderData.address,
		remark: orderData.remark
	});
};

// 示例8: 自定义Content-Type的请求
export const uploadData = (data) => {
	return API.request('/api/upload', data, 'POST', {
		contentType: 'application/x-www-form-urlencoded'
	});
};

// 示例9: 带自定义请求头的请求
export const customRequest = (data) => {
	return API.request('/api/custom', data, 'POST', {
		header: {
			'X-Custom-Header': 'custom-value'
		}
	});
};

// 使用示例
export const exampleUsage = async () => {
	try {
		console.log('=== API签名算法示例 ===');
		
		// 1. GET请求示例
		console.log('1. GET请求 - 获取用户信息');
		const userInfo = await getUserInfo(123);
		console.log('用户信息:', userInfo);
		
		// 2. POST请求示例（JSON）
		console.log('2. POST请求（JSON）- 用户登录');
		const loginResult = await login('testuser', '123456');
		console.log('登录结果:', loginResult);
		
		// 3. POST请求示例（表单）
		console.log('3. POST请求（表单）- 表单登录');
		const formLoginResult = await loginForm('testuser', '123456');
		console.log('表单登录结果:', formLoginResult);
		
		// 4. 带查询参数的GET请求
		console.log('4. 带查询参数的GET请求 - 获取订单列表');
		const orders = await getOrders(1, 10, 'pending');
		console.log('订单列表:', orders);
		
		// 5. 复杂POST请求
		console.log('5. 复杂POST请求 - 创建订单');
		const orderData = {
			goods: [{ id: 1, quantity: 2 }],
			address: { name: '张三', phone: '13800138000' },
			remark: '请尽快发货'
		};
		const orderResult = await createOrder(orderData);
		console.log('订单创建结果:', orderResult);
		
	} catch (error) {
		console.error('API调用失败:', error);
	}
};

// 签名算法测试函数
export const testSignature = () => {
	console.log('=== 签名算法测试 ===');
	
	// 模拟签名过程
	const route = '/api/xxxOpenApi/list';
	const timestamp = '1640995200';
	const queryParams = { para: ['1', '2'] }; // 注意：这里para有两个值
	const bodyContent = '{"a":"1","b":"2"}';
	const secretKey = 'your_secret_key';
	const tenantId = '734476481183813';
	const authorization = 'Bearer your_jwt_token';
	
	// 第一次签名
	const queryString = SignatureUtils.sortParams(queryParams);
	const firstSignString = `${route}&${timestamp}&${queryString}&${bodyContent}&key=${secretKey}`;
	console.log('第一次签名字符串:', firstSignString);
	
	// 最终签名
	const finalSignString = `${tenantId}${firstSign}&${timestamp}&${authorization}`;
	console.log('最终签名字符串:', finalSignString);
};
